from ossutil import OSSClient
import pandas as pd
import config
import os
import datetime
import dbtool
import loaddata
import errorcode
from lib.datatypeutil import df_decimal_to_float
import re

# function
def remove_sign(sql):
    """
    功能：去掉sql语句末尾的;
    :param sql: sql语句
    :return: string
    """
    pattern = '.*;$'
    if re.match(pattern, sql):
        sql = re.sub(';$', '', sql)
    return sql

"""
将从数据库获取的数据上传至oss
"""
def data_upload(data, file_name):
    """
    功能：将数据上传至oss
    :param data: 从数据库读取的数据, pd.DataFrame格式
    :param file_name: 存入本地的数据名字
    :return:
    """

    #0 file_path构造
    file_path = os.path.join(os.getcwd(), 'data', file_name)

    #1 将DataFrame格式的data存入本地
    data.to_csv(file_path, index = False, encoding = 'utf-8')

    #2 将数据上传至oss
    oss_client = OSSClient(app_domain=config.app_domain,
                           auth_domain=config.auth_domain,
                           app_id=config.app_id,
                           app_key=config.app_key,
                           bucket=config.bucket)
    oss_client.upload(file_name, file_path)

def generate_number():
    """
    功能：通过时间戳生成随机数
    :return:int
    """
    now = datetime.datetime.now()
    timestamp_int = int(now.timestamp())
    return timestamp_int

def get_data_from_database(dao, conn_id, sql_syntax):
    """
    功能：从数据库获取数据（hive、mysql）
    :param dao: 连接数据库
    :param conn_id: 目标数据库信息id
    :param sql_id: sql语句
    :return: DataFrame格式数据
    """

    # 连接数据库
    # 0、获取数据连接参数
    #conn_id = int(conn_id)
    sql_conn = 'select * from {connect_tabel} where id = {id}'.format(connect_tabel=config.connect_tabel,
                                                                          id = conn_id)
    database_inf = dao.query_data(sql = sql_conn)
    database_inf = database_inf.reset_index(drop = True)
    database_inf = database_inf.iloc[0, :]

    # 1、获取sql语句
    sql_data = sql_syntax

    # 2、连接数据库
    paltform = database_inf['platform']  # 判断是哪种数据库类型
    dao_database = dbtool.Dao(host=database_inf['host'], port=int(database_inf['port']), user=database_inf['user'],
                              database=database_inf['db'], password=database_inf['password'])

    # 3、取出数据
    if paltform == 'mysql':
        data = dao_database.query_data(sql = sql_data)
    else:  # hive
        data = dao_database.query_data_hive(sql = sql_data)

    # 4、Decimal格式数据转换
    data = df_decimal_to_float(data)
    return data

def get_data_from_oss(file_name, file_size):
    #0、连接oss
    oss_client = OSSClient(app_domain=config.app_domain,
                           auth_domain=config.auth_domain,
                           app_id=config.app_id,
                           app_key=config.app_key,
                           bucket=config.bucket)
    # 1、将数据下载到本地
    download_name = str(file_size) + str(file_name)
    try:
        download_path = os.path.join(os.path.dirname(os.path.abspath('__file__')), 'data',
                                     download_name)  # 构造下载数据的路径，默认为当前工作目录，文件名和数据库的保持一致
        oss_client.download(download_name, download_path)  # 会将数据保存到当前目录下
    except Exception as e:
        error_code_msg = '1003' + ':' + errorcode.Data_Read_1003
        raise Exception(error_code_msg)

    # 2、读取当前目录下的数据
    data = None
    try:
        ld = loaddata.LoadData()
        data = ld.get_local_data(download_path)
    except Exception as e:
        error_code_msg = '1001' + ':' + errorcode.Data_Read_1001
        raise Exception(error_code_msg)

    return data


if __name__ == '__main__':
    #test
    data = pd.DataFrame({'id': ['A', 'A', 'B'] * 3, 'value': [1, 2, 3] * 3})
    file_name = 'data_upload_test_3.csv'
    data_upload(data, file_name)


